<?php

namespace App\Admin\Controllers;

use App\Models\Administrator;
use App\Models\Machine;
use App\Models\Proxy;
use App\Models\Team;
use App\Models\TeamLeader;
use Encore\Admin\Auth\Database\Permission;
use Encore\Admin\Auth\Database\Role;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Routing\Controller;

class TeamLeadersController extends Controller
{
    use ModelForm;

    protected $header = '团体管理员';
    protected $description = '管理团体下用户咖啡券分发和审核的管理员';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->form()->edit($id));
        });

    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(TeamLeader::class, function (Grid $grid) {
            $grid->model()->where('type', Administrator::TEAM_LEADER);
            $grid->team()->name('团体');
            $grid->username(trans('admin.username'));
            $grid->name(trans('admin.name'));
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if ($actions->getKey() == 1) {
                    $actions->disableDelete();
                }
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });

            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(TeamLeader::class, function (Form $form) {

            $form->text('username', trans('admin.username'))->rules('required');
            $form->password('password', trans('admin.password'))->rules('required|confirmed');
            $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
                ->default(function ($form) {
                    return $form->model()->password;
                });
            $form->text('name', trans('admin.name'))->rules('required');

            $teams = Team::query()->pluck('name', 'id')->toArray();
            $form->select('team_id', '团体')->options($teams);
            $form->multipleSelect('roles', trans('admin.roles'))->options(Role::query()->where('slug', Administrator::TEAM_LEADER)->pluck('name', 'id'));
            $form->hidden('type')->value(Administrator::TEAM_LEADER);
            $form->image('avatar', trans('admin.avatar'));

            $form->ignore(['password_confirmation']);

            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }
            });

        });
    }
}
